package {
	import flash.display.StageQuality;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import org.papervision3d.objects.DisplayObject3D;
	
	import org.PaperBase.PaperBase;
	import org.ascollada.utils.FPS;
	import org.papervision3d.core.math.Number3D;
	import org.papervision3d.core.math.Plane3D;
	import org.papervision3d.core.utils.InteractiveSceneManager;
	import org.papervision3d.events.InteractiveScene3DEvent;
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.objects.primitives.Plane;
	
	import caurina.transitions.*;

	//[SWF(width="1280", height="800", backgroundColor="0x000000", frameRate="60")]
	[SWF(width="800", height="600", backgroundColor="0x000000", frameRate="60")]
	public class mmss_3D extends PaperBase
	{
	
		private var _fps:FPS = new FPS();	//Anzeige der Frames/Secound
		private var Polaroids:Array = new Array();
		private var activeRondell:Rondell;
		private var hitObject:Object;
		private var dragging:Boolean = false;
		private var rotating:Boolean = false;
		private var max:Boolean = false;
		private var popRondell:Boolean = false;
		private var countRondell:int = 0;
		
		public static var txt:TextField;
		public static var txtLevel:TextField;
		
		private var base_plane:Plane3D;
		private var schnitt:Plane; 

		/**
		 * Im Konstruktor ist nichts weiter zu tun als die init() Funktion aufzurufen. 
		 * Die Init-Funtion kommt aus der Paperbase und inititalisiert den
		 * Viewport,Kamera und den Renderer
		 */
		public function mmss_3D()
		{
			
			
			stage.quality = StageQuality.BEST;
			
			//init(1280,800);		
			init();
			
			default_camera.z = -2500;
			default_camera.y = 725;	
			default_camera.zoom = 5;
		}
		
		/**
		 * Hier werden alle 2D Objekte initialisiert 
		 */
		override protected function init2d():void{
			
			var tmpBackground:Background = new Background();
			tmpBackground.createLinear(stage.stageWidth,stage.stageHeight/2);
			//tmpBackground.y = (stage.stageHeight/4)*3;
			//tmpBackground.y += 65;
			addChild(tmpBackground);
			
			addChild(_fps);
			_fps.y += 580;
		
			//Textfeld zum debuggen
			txt = new TextField();
			txt.height = 800;
			txt.selectable = false;
			txt.textColor = 0xffffff;
	        addChild(txt);
			
			txt.text = "Menu Level 1";
			
			for each(var tmpP:Polaroid in this.Polaroids){
				tmpP.enableInteractive();
			}
			activeRondell.enableInteractive();
			
			var ism:InteractiveSceneManager = viewport.interactiveSceneManager;
			ism.addEventListener(InteractiveScene3DEvent.OBJECT_PRESS, hitHandle);
			
			//stage.addEventListener(MouseEvent.MOUSE_DOWN, startDragr);
			stage.addEventListener(MouseEvent.MOUSE_UP,stopDragr);	
			stage.addEventListener(MouseEvent.MOUSE_WHEEL, onWheel);
		}

		
		/**
		 * Hier werden alle 3D Objekte initialisiert 
		 * Es werden hier alle wichtigen 3D-Einstellungen vorgenommen
		 */
		override protected function init3d():void{
			
			//baseplane ausrichten
			base_plane = new Plane3D();
			base_plane.setNormalAndPoint(new Number3D(0,1,0),new Number3D(0,0,0));
			
			
			schnitt = new Plane(new ColorMaterial(0xff0000),50,50,10,10);
			schnitt.x=0;
			schnitt.y=0;
			schnitt.z = 0;
			schnitt.pitch(90);
			schnitt.alpha = 30;
			schnitt.visible = false;
			
			default_scene.addChild(schnitt);
			
			/*
			for(var i:uint = 0; i < 10; i++){
				var tmpPolaroid:Polaroid = new Polaroid();
				tmpPolaroid.z = Math.random() *10000  -1400;
				tmpPolaroid.y = 1250;
				tmpPolaroid.x = Math.random() * 2000 - 1000;
				default_scene.addChild(tmpPolaroid);
				this.Polaroids.push(tmpPolaroid);
				//tweenMe(tmpPolaroid);
			}*/
			/*
			var tmpVPolaroid:VideoPolaroid = new VideoPolaroid();
			tmpVPolaroid.z = -1400;
			tmpVPolaroid.y = 1250;
			default_scene.addChild(tmpVPolaroid);
			this.Polaroids.push(tmpVPolaroid);
			*/
			/*
			var tmpDPolaroid:DocumentPolaroid = new DocumentPolaroid(stage);
			tmpDPolaroid.z = -1500;
			tmpDPolaroid.y = 1250;
			default_scene.addChild(tmpDPolaroid);
			this.Polaroids.push(tmpDPolaroid);
			*/
			activeRondell = new Rondell(10);
			
			activeRondell.x = 0 - activeRondell.getRadius/4;
			activeRondell.y = 0;
			activeRondell.z = 500 + activeRondell.getRadius / 2 ;
			
			default_scene.addChild(activeRondell);
			
			
		}
			
		private function onWheel(e:MouseEvent):void{
			if(this.hitObject != null){
				txt.text = "scroll";
				this.hitObject.scroll(e.delta,e.ctrlKey);
			}
		}

		public function startDragr(e:MouseEvent):void {
			trace("startDragr");
			
			stage.removeEventListener(MouseEvent.MOUSE_DOWN,startDragr);
			stage.addEventListener(MouseEvent.MOUSE_UP,stopDragr);	
			
		}
		public function stopDragr(e:MouseEvent):void {
			
			trace("stopDragr");
			
			//stage.addEventListener(MouseEvent.MOUSE_DOWN,startDragr);
			//stage.removeEventListener(MouseEvent.MOUSE_UP, stopDragr);	
			
			this.dragging = false;
			this.rotating = false;
			schnitt.visible = false;
		}
		public function hitHandle(e:InteractiveScene3DEvent):void {
			trace("hit handle");
			
			/*var ism:InteractiveSceneManager = viewport.interactiveSceneManager;
			ism.removeEventListener(InteractiveScene3DEvent.OBJECT_PRESS,hitHandle);

			activeRondell.disableInteractive();
			
			for each(var disPolaroid:Polaroid in this.Polaroids){
					disPolaroid.disableInteractive();
			}*/
			
			if (e.displayObject3D != null) {
				
				//rondell???
				if (e.displayObject3D.parentContainer.parentContainer == activeRondell) {
					if(e.displayObject3D.name.match("content")){
							this.popRondell = true;
							this.countRondell++;
							trace("rondell");
					}
				}
				
				for each(var tmpP:Polaroid in this.Polaroids){
					
					if(e.displayObject3D.parentContainer == tmpP){
						this.hitObject = tmpP;
						
						if(e.displayObject3D.name.match("drag")){			
							this.dragging = true;
							schnitt.visible = true;
						}
						else if(e.displayObject3D.name.match("rotate")){
							this.rotating = true;
						}
						else if(e.displayObject3D.name.match("max")){
							this.max = true;
						}
						break;
					}
				}
			}
		}
		public function tweenMe(scope:DisplayObject3D):void {
			
			Tweener.addTween(scope, {
					x: Math.random() * 5000 - 1000,
					z: Math.random() * 15000  -500,
					rotationY: 360,
					time:2,
					delay: Math.random() * 5,
					transition:"easeInOutBack",
					onComplete: function():void {
									scope.rotationY = 0;
									tweenMe(scope);
								}
					});
			
		}
		
		public function tweenRondell():void {
			if(this.countRondell<3){
					this.popRondell = false;
					Tweener.addTween(activeRondell, {
						x:0,
						y:5000,
						z: -1700,
						time:2,
						transition:"easeInOutBack",
						onComplete: function():void {
							this.changeRondell(countRondell);
							activeRondell.enableInteractive();
						}
					});
					Tweener.addTween(activeRondell, {
						delay:2,
						x:0 - activeRondell.getRadius/4,
						y:0,
						z: 500 + activeRondell.getRadius / 2,
						time:2,
						transition:"easeInOutBack"
					});
						
					txt.text = "Menu Level " + (countRondell+1);
					
			}
			else {
				this.popRondell = false;
				
				Tweener.addTween(activeRondell, {
						x:0,
						y:5000,
						z: -1700,
						time:2,
						transition:"easeInOutBack",
						onComplete: function():void {
							//trace(activeRondell.getObjects());
							addElements(activeRondell.getObjects());
							default_scene.removeChild(activeRondell);
						}
				});
				
				txt.text = "Media Merge Study Space";
			}
		}
		
		public function addElements(objects:Array):void {
			for each (var item:Polaroid in objects) {
				Polaroids.push(item);
				default_scene.addChild(item);
				item.x = 0;
				item.y = 5000;
				item.z = -1700;
				item.rotationY = 0;
				Tweener.addTween(item, {
						scale:1,
						z:Math.random() *10000  -1400,
						y:1250,
						x:Math.random() * 2000 - 1000,
						time:2,
						transition:"easeInOutBack"
				});
			}		
		}
		
		/**
		 * Wird vom OnEnterFrame des Renderes ausgeführt
		 * Alle Rendervorgänge werden hier eingetragen
		 */override protected function processFrame():void {	
			if(this.dragging){
				//Dragstuff
				//update camera position
				var cameraPosition:Number3D = new Number3D(default_camera.x, default_camera.y, default_camera.z);
				//get the direction vector of the mouse position
				var ray:Number3D = default_camera.unproject(viewport.containerSprite.mouseX, viewport.containerSprite.mouseY);
				//convert ray to a 3d point in the ray direction from the camera
				ray = Number3D.add(ray, cameraPosition);
				//find the intersection of the line defined by the camera and the ray position with the plane3D
				var intersect:Number3D = base_plane.getIntersectionLineNumbers(cameraPosition, ray);
				
				schnitt.x = intersect.x;
				schnitt.z = intersect.z;
				schnitt.y = intersect.y;
				
				/*
				txt.text=
					"MX: "+stage.mouseX+"\nMY: "+stage.mouseY
					+"\nCamx: "+cameraPosition.x+"\nCamy: "+cameraPosition.y+"\nCamz: "+cameraPosition.z
					+"\nIx: " + intersect.x + "\nIy: " + intersect.y + "\nIz: " + intersect.z + "\n"
					+"\nHx: " + hitObject.x + "\nHy: " + hitObject.y + "\nHz: " + hitObject.z + "\n";
				*/
					
				
				//dragbewegung
				this.hitObject.dragIt(intersect.x,intersect.z,intersect.y);
			}
			else if(this.rotating){
				this.hitObject.rotateIt([stage.mouseX,stage.width]);
			}
			else if (this.max) {
				this.max = false;
				this.hitObject.rotationY = 0;
				Tweener.addTween(this.hitObject, {
					x:0,
					z: -1700,
					rotationY: 360,
					time:2,
					transition:"easeInOutBack"
					});
			}
			else if (this.popRondell) {
				this.tweenRondell();
			}
			
			activeRondell.process(stage.mouseX, stage.width);
			
		}
	}
		
}

